import random

import pygame.display, time

from bullet import Bullet
from enemyTank import EnemyTank
from myTank import MyTank
from wall import Wall

_display = pygame.display
_surface = pygame.Surface

color_black = pygame.Color(0, 0, 0)
color_red = pygame.Color(255, 0, 0)

screen_width = 800
screen_height = 500


class MainGame:
    # 游戏主窗口
    window = None
    # 创建我的坦克
    my_tank = None
    # 敌方坦克、
    ey_tank_count = 5
    ey_tank_list = []
    # 存储子弹
    my_bullet_list = []
    ey_bullet_list = []
    # 爆炸效果
    explode_list = []
    # 墙壁
    wall_list = []
    def __init__(self):
        pass

    def start_game(self):
        _display.init()
        # 创建窗口，
        MainGame.window = _display.set_mode(size=(screen_width, screen_height))
        # 创建我的tank
        MainGame.my_tank = MyTank()
        # 创建敌方tank
        self.create_enemy_tank()

        # 展示墙壁
        self.create_wall()
        # 设置游戏标题
        _display.set_caption("Space Tank Game")
        # 让窗口持续刷新
        while True:
            # 给窗口填充颜色
            MainGame.window.fill(color=color_black)
            # 事件处理
            self.get_event()
            # 将绘制文字的画布粘贴到窗口
            text_surface = self.get_text_surface("敌方坦克总共%d辆, 剩余%d辆" % (MainGame.ey_tank_count, len(MainGame.ey_tank_list)))
            MainGame.window.blit(text_surface, dest=(0, 0))
            if MainGame.my_tank and not MainGame.my_tank.stop:
                # 判断是否发生碰撞
                MainGame.my_tank.move(screen_width, screen_height)
                MainGame.my_tank.hit(MainGame.ey_tank_list)
            # 将我的tank加入到窗口
            if MainGame.my_tank and MainGame.my_tank.live:
                MainGame.my_tank.display(MainGame.window)
            else:
                del MainGame.my_tank
                MainGame.my_tank = None
            # 子弹射击-我方
            self.blit_my_bullet()
            # 子弹射击-敌方
            self.blit_ey_bullet()
            # 加入敌方tank
            self.blit_enemy_tank()
            # 爆炸效果
            self.blit_explode()
            # 展示墙壁
            self.blit_wall()
            # 控制刷新频率
            time.sleep(0.03)
            # 持续更新
            _display.update()

    def end_game(self):
        print("游戏结束")
        # 结束python解释器
        exit()

    def get_event(self):
        # 1.获取所有事件
        eventList = pygame.event.get()
        # 2.对事件进行判断处理（1、点击关闭按钮，2、键盘按键）
        for event in eventList:
            if event.type == pygame.QUIT:
                self.end_game()
            # 判断事件类型是否为安检
            if event.type == pygame.KEYDOWN:
                if MainGame.my_tank and MainGame.my_tank.live:
                    if event.key == pygame.K_LEFT:
                        print("坦克向左移动")
                        # 修改tank方向
                        MainGame.my_tank.direction = "L"
                        MainGame.my_tank.stop = False
                        # MainGame.my_tank.move(screen_width)
                    elif event.key == pygame.K_RIGHT:
                        print("坦克向右移动")
                        MainGame.my_tank.direction = "R"
                        MainGame.my_tank.stop = False
                        # MainGame.my_tank.move(screen_width)
                    elif event.key == pygame.K_UP:
                        print("坦克向上移动")
                        MainGame.my_tank.direction = "U"
                        MainGame.my_tank.stop = False
                        # MainGame.my_tank.move(screen_height)
                    elif event.key == pygame.K_DOWN:
                        print("坦克向下移动")
                        MainGame.my_tank.direction = "D"
                        MainGame.my_tank.stop = False
                        # MainGame.my_tank.move(screen_height)
                    elif event.key == pygame.K_SPACE:
                        if len(MainGame.my_bullet_list) < 3:
                            b = Bullet(MainGame.my_tank)
                            MainGame.my_bullet_list.append(b)
                        else:
                            print("子弹不足")
            if event.type == pygame.KEYUP:
                if MainGame.my_tank and MainGame.my_tank.live:
                    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                        MainGame.my_tank.stop = True

    def get_text_surface(self, text):
        # 初始化字体模块
        pygame.font.init()
        # 选中合适的字体
        font = pygame.font.SysFont("kaiti", 16)
        # 使用字体完成绘制
        return font.render(text, True, color_red)

    # 创建敌方tank
    def create_enemy_tank(self):
        for i in range(MainGame.ey_tank_count):
            ey_tank = EnemyTank(screen_width, screen_height)
            MainGame.ey_tank_list.append(ey_tank)

    # 循环展示敌方tank
    def blit_enemy_tank(self):
        for eTank in MainGame.ey_tank_list:
            if eTank.live:
                eTank.display(MainGame.window)
                eTank.rand_move(screen_width, screen_height)
                eTank.hit(MainGame.ey_tank_list)
                # 射击
                bullet = eTank.shot()
                if bullet:
                    MainGame.ey_bullet_list.append(bullet)
            else:
                MainGame.ey_tank_list.remove(eTank)

    # 将子弹渲染到窗口
    def blit_my_bullet(self):
        # 我放子弹
        for bullet in MainGame.my_bullet_list:
            if bullet.live:
                bullet.display(MainGame.window)
                bullet.move(screen_width, screen_height)
                # 检测子弹与敌方tank是否碰撞
                bullet.hit_ey_tank(MainGame.ey_tank_list,MainGame.explode_list)
            else:
                MainGame.my_bullet_list.remove(bullet)

    def blit_ey_bullet(self):
        # 敌放子弹
        for bullet in MainGame.ey_bullet_list:
            if bullet.live:
                bullet.display(MainGame.window)
                bullet.move(screen_width, screen_height)
                if MainGame.my_tank and MainGame.my_tank.live:
                    bullet.hit_my_tank(MainGame.my_tank,MainGame.explode_list)
            else:
                MainGame.ey_bullet_list.remove(bullet)

    # 展示爆炸效果
    def blit_explode(self):
        for explode in MainGame.explode_list:
            if explode.live:
                explode.display(MainGame.window)
            else:
                MainGame.explode_list.remove(explode)

    def create_wall(self):
        for i in range(5):
            wall = Wall(random.randint(0,screen_width),random.randint(0,screen_height))
            MainGame.wall_list.append(wall)

    def blit_wall(self):
        for wall in MainGame.wall_list:
            wall.display(MainGame.window)

if __name__ == "__main__":
    MainGame().start_game()
